import os

def modify_cflags_in_makefile(folder_path, new_cflags):
    
    for root, dirs, files in os.walk(folder_path):
        
        for file in files:
            
            if file == 'Makefile':
                
                makefile_path = os.path.join(root, file)
                with open(makefile_path, 'r') as f:
                    lines = f.readlines()
                with open(makefile_path, 'w') as f:
                    for line in lines:
                        if line.startswith('CFLAGS='):
                            f.write('CFLAGS={}\n'.format(new_cflags))
                        else:
                            f.write(line)

        
        for dir in dirs:
            dir_path = os.path.join(root, dir)
            
            modify_cflags_in_makefile(dir_path, new_cflags)


folder_path = '/home/zb/Dateset/C/testcases'
new_cflags = '-c -O1'


modify_cflags_in_makefile(folder_path, new_cflags)
